import { userStore } from "@/stores/user";
import { generator } from "@/utils/generatorRouters";
import { markRaw } from "vue";
import { createRouter, createWebHistory } from "vue-router";

import { routes } from "./common/index";

const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  if (localStorage.getItem("app_token")) {
    const menu = userStore().menu;
    console.log(menu);

    const routeArr = generator(menu);
    console.log("///", routeArr);

    routeArr.map((item: any) => {
      router.addRoute("index", item);
    });
    next();
  } else {
    if (to.path !== "/auth/login") {
      next({ path: "/auth/login", query: { redirect: to.fullPath } });
    } else {
      next();
    }
  }
});

export default router;
